﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace A2DFramework.SecurityService.Configuration
{
    internal static class SecurityConfiguration
    {
        static SecurityConfiguration()
        {
            Load();
        }
        internal static void Load()
        {
            if (_loaded)
                return;

            XElement root = XElement.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "A2D.config"));

            root = root.Element("SecurityConfig");
            if (root == null)
                return;

            _DDoSProtect_MaxRequestsPerCheck = int.Parse(root.Element("DDoSProtect").Element("MaxRequestsPerCheck").Value);
            _DDoSProtect_CheckInterval = int.Parse(root.Element("DDoSProtect").Element("CheckInterval").Value);
            _DDoSProtect_BlockTimeout = int.Parse(root.Element("DDoSProtect").Element("BlockTimeout").Value);
            
            _DDoSProtect_IgnoreRequestsExtentions = new List<string>();
            string exts = root.Element("DDoSProtect").Element("IgnoreRequestsExtentions").Value;
            exts = exts ?? "";
            string[] extArray=exts.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach(var ext in extArray)
                _DDoSProtect_IgnoreRequestsExtentions.Add(ext.ToLower().Trim());

            _loaded = true;
        }
        private static bool _loaded = false;
        private static int _DDoSProtect_MaxRequestsPerCheck { get; set; }
        private static int _DDoSProtect_CheckInterval { get; set; }
        private static int _DDoSProtect_BlockTimeout { get; set; }
        private static List<string> _DDoSProtect_IgnoreRequestsExtentions { get; set; }

        public static int DDoSProtect_MaxRequestsPerCheck
        {
            get
            {
                if (!_loaded)
                    Load();
                return _DDoSProtect_MaxRequestsPerCheck;
            }
        }
        public static int DDoSProtect_CheckInterval
        {
            get
            {
                if (!_loaded)
                    Load();
                return _DDoSProtect_CheckInterval;
            }
        }
        public static int DDoSProtect_BlockTimeout
        {
            get
            {
                if (!_loaded)
                    Load();
                return _DDoSProtect_BlockTimeout;
            }
        }
        public static List<string> DDoSProtect_IgnoreRequestsExtentions
        {
            get
            {
                if (!_loaded)
                    Load();
                return _DDoSProtect_IgnoreRequestsExtentions;
            }
        }
    }
}
